package com.example.cardgame

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity

class FirstActivity : AppCompatActivity() {
    @SuppressLint("MissingInflatedId", "WrongViewCast")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_first)

        val name = findViewById<EditText>(R.id.editTextText)
        val ID = findViewById<EditText>(R.id.editTextText3)
        val number = findViewById<EditText>(R.id.editTextText4)
        val email = findViewById<EditText>(R.id.editTextText5)
        val save = findViewById<Button>(R.id.yes)
        val trip = findViewById<CheckBox>(R.id.trip)
        val read = findViewById<CheckBox>(R.id.read)
        val game = findViewById<CheckBox>(R.id.Game)
        val shopping = findViewById<CheckBox>(R.id.shopping)
        val radioGroup = findViewById<RadioGroup>(R.id.gender)

        var selectedGender = ""
        radioGroup.setOnCheckedChangeListener { _, checkedId ->
            val radio: RadioButton = findViewById(checkedId)
            selectedGender = radio.text.toString()
        }

        save.setOnClickListener {
            KEY2 = "" // 清空 KEY2
            val intent = Intent() // 确保这里指向下一个活动
            intent.putExtra("name", name.text.toString())
            intent.putExtra("id", ID.text.toString())
            intent.putExtra("number", number.text.toString())
            intent.putExtra("email", email.text.toString())
            intent.putExtra("sex", selectedGender)
            if (trip?.isChecked == true) {
                KEY2 += "${trip.text} "
            }
            if (read?.isChecked == true) {
                KEY2 += "${read.text} "
            }
            if (game?.isChecked == true) {
                KEY2 += "${game.text} "
            }
            if (shopping?.isChecked == true) {
                KEY2 += "${shopping.text} "
            }

            intent.putExtra("hobby", KEY2)
            setResult(1, intent)
            finish()
        }

        val cancel = findViewById<Button>(R.id.cancel)
        cancel.setOnClickListener {
            finish()
        }
    }
}